
module.exports = function(config, app) {

    return async (ctx, next) => {
        try {
            await next();
        } catch (e) {
            const code = Number(e.code);
            if (code) {
                ctx.logger.error(`API error, code: ${code},`, e);
                ctx.body = {
                    code: code,
                    message: e.message
                };
            } else {
                throw e;
            }
        }
    }
}
